package com.jinsp.cameralibrary;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by luoshuai on 1/11/22
 * <p>
 * 绘制指定形状的ImageView，支持圆形以及圆角矩形
 */
public class ShapeImageView extends android.support.v7.widget.AppCompatImageView {

    public static final int SHAPE_DEFAULT = -1; //默认形状
    public static final int SHAPE_CIRCLE = 0;   //圆形
    public static final int SHAPE_ROUND = 1;    //圆角

    private int shape = SHAPE_DEFAULT;
    private final Paint mBitmapPaint;       //bitmap画笔
    private final Paint mBorderPaint;       //边框画笔
    private int borderDrawable;             //边框drawable
    private final Matrix matrix = new Matrix();
    private BitmapShader bitmapShader;
    private Bitmap preBitmap;               //上一张bitmap
    private boolean isDrawBorder;           //是否绘制边框
    private float borderWidth;              //边框宽度
    private final RectF mBorderRect, mBitmapRect; //边框、bitmap矩形
    private int rounderRadius;               //圆角半径

    public ShapeImageView(Context context) {
        this(context, null);
    }

    public ShapeImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ShapeImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mBitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBorderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBorderPaint.setStyle(Paint.Style.FILL);
        mBorderPaint.setColor(Color.WHITE);
        borderWidth = 10;
        rounderRadius = 15;
        isDrawBorder = true;
        mBorderPaint.setStrokeWidth(borderWidth);
        mBorderRect = new RectF();
        mBitmapRect = new RectF();
        //禁用硬件加速
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    /**
     * 设置ImageView形状
     *
     * @param shape 形状
     */
    public void setShape(int shape) {
        this.shape = shape;
    }

    /**
     * 设置圆角矩形半径
     *
     * @param rounderRadius 圆角半径
     */
    public void setRounderRadius(int rounderRadius) {
        this.rounderRadius = rounderRadius;
    }

    /**
     * 设置边框宽度
     *
     * @param borderWidth 宽度
     */
    public void setBorderWidth(float borderWidth) {
        this.borderWidth = borderWidth;
        mBorderPaint.setStrokeWidth(borderWidth);
    }

    /**
     * 设置边框颜色
     *
     * @param borderColor 边框颜色
     */
    public void setBorderColor(int borderColor) {
        mBorderPaint.setColor(borderColor);
    }

    /**
     * 设置边框drawable，边框颜色以及宽度失效
     *
     * @param borderDrawable 边框drawable
     */
    public void setBorderDrawable(int borderDrawable) {
        this.borderDrawable = borderDrawable;
    }

    /**
     * 是否绘制边框
     *
     * @param isDrawBorder true -- 绘制边框
     */
    public void isDrawBorder(boolean isDrawBorder) {
        this.isDrawBorder = isDrawBorder;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (shape == SHAPE_CIRCLE || shape == SHAPE_ROUND) {
            Drawable srcDrawable = getDrawable();
            if (srcDrawable instanceof BitmapDrawable) {
                //获取设置的图
                BitmapDrawable bitmapDrawable = (BitmapDrawable) srcDrawable;
                Bitmap targetBitmap = bitmapDrawable.getBitmap();
                if (targetBitmap != preBitmap) {
                    preBitmap = targetBitmap;
                    bitmapShader = null;
                }
                if (bitmapShader == null) {
                    bitmapShader = new BitmapShader(targetBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
                }
                //缩放bitmap
                int imageViewWidth = getWidth();
                int imageViewHeight = getHeight();
                int minWH = Math.min(imageViewWidth, imageViewHeight);
                int realImageViewWidth = minWH;
                int realImageViewHeight = minWH;
                if (shape == SHAPE_ROUND) {
                    realImageViewWidth = imageViewWidth;
                    realImageViewHeight = imageViewHeight;
                }
                matrix.setScale((realImageViewWidth - borderWidth * 2) / targetBitmap.getWidth(),
                        (realImageViewHeight - borderWidth * 2) / targetBitmap.getHeight());
                bitmapShader.setLocalMatrix(matrix);
                mBitmapPaint.setShader(bitmapShader);
                if (shape == SHAPE_CIRCLE) {
                    float radius = minWH >> 1;
                    if (isDrawBorder) {
                        //绘制边框
                        canvas.drawCircle(radius, radius, radius - borderWidth / 2, mBorderPaint);
                        canvas.translate(borderWidth, borderWidth);
                        //绘制bitmap
                        canvas.drawCircle(radius - borderWidth, radius - borderWidth,
                                radius - borderWidth, mBitmapPaint);
                    } else {
                        canvas.drawCircle(radius, radius, radius, mBitmapPaint);
                    }
                } else if (shape == SHAPE_ROUND) {
                    if (isDrawBorder) {
                        //绘制边框
                        mBorderRect.set(borderWidth / 2, borderWidth / 2,
                                realImageViewWidth - borderWidth / 2, realImageViewHeight - borderWidth / 2);
                        mBitmapRect.set(0, 0, realImageViewWidth - borderWidth * 2,
                                realImageViewHeight - borderWidth * 2);
                        float borderRadius = rounderRadius - borderWidth / 2;
                        borderRadius = Math.max(0, borderRadius);
                        float bitmapRadius = rounderRadius - borderWidth;
                        bitmapRadius = Math.max(0, bitmapRadius);
                        canvas.drawRoundRect(mBorderRect, borderRadius, borderRadius, mBorderPaint);
                        canvas.translate(borderWidth, borderWidth);
                        //绘制bitmap
                        canvas.drawRoundRect(mBitmapRect, bitmapRadius, bitmapRadius, mBitmapPaint);
                    } else {
                        canvas.drawRoundRect(0, 0, realImageViewWidth, realImageViewHeight,
                                rounderRadius, rounderRadius, mBitmapPaint);
                    }
                }

                return;
            }
        }
        super.onDraw(canvas);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (preBitmap != null) {
            preBitmap.recycle();
            preBitmap = null;
        }
    }
}
